# flask web 网络编程文件
from flask import Flask, render_template, request,escape,flash,session,redirect
from geo import geocode
from walk import bus
import youhua
from request import log_request

高德api_key = "33c844caa1de284dd24d8113e95464af"
# html 调用方法：render_template  按照 jinja2 规则


app = Flask(__name__)


@app.route('/search4', methods=['POST'])
# 这些数据来自哪里？
def do_search() -> 'html':
    color01 = request.form['color01']
    color02 = request.form['color02']
    start_address=request.form['start-bus']
    end_address=request.form['end-bus']
    city_address=request.form['city-bus']
    title = '查询结果'
    df = youhua.direction_bus(高德api_key, start_address, end_address,city_address)
    results = df.loc[:, ['name', 'type', 'distance','duration','departure_stop.name','arrival_stop.name']]
    results.columns = ['名称', '类型', '距离','持续时间','出发站点','到达站点']
    total_bus = youhua.total_bus(df)
    total_bus_time = youhua.total_bus_time(df)
    route = results['名称'].tolist()
    log_request(request, route)
    return render_template('results.html',
                           the_title=title,
                           the_results=results,
                           the_color01=color01,
                           the_color02=color02,
                           the_start_address=start_address,
                           the_end_address=end_address,
                           the_city_address=city_address,
                           the_html_table=results.to_html(index=False),
                           the_total_bus=total_bus,
                           the_total_bus_time=total_bus_time,
                           )


#用户日志的管理页面
@app.route('/viewlog')
def view_the_log() -> 'html':
    contents = []
    with open('vsearch.log', 'r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    titles = ('用户输入数据', '远程地址', '用户代理', '查询结果')
    return render_template('log.html',
                           the_title='查看日志',
                           the_row_titles=titles,
                           the_data=contents,)



@app.route('/')
def entry_page() -> 'html':
    title = '公交出行规划'
    return render_template('entry.html',
                           the_title=title,)




if __name__ == "__main__":
        app.run(port=666)



